package com.cta.service;

import java.util.Calendar;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Service;

import com.cta.utils.CodeLookupUtils;

@Service
public class RecoveryLogService {

	protected static final Logger log = LoggerFactory.getLogger(RecoveryLogService.class);

	public static final int MISSING_CONFIG = 1;
	public static final int INSERT_FAIL = 2;
	public static final int CALCULATE_FAIL = 3;

	private static Date missSent;
	private static Date insertSent;
	private static Date calculateSent;

	private static MailSender mailSender;

	@Autowired(required = true)
	public void setMailSenderService(MailSender mailSender) {
		RecoveryLogService.mailSender = mailSender;
	}

	private static SimpleMailMessage alertMailMessage;

	@Autowired(required = true)
	public void setSimpleMailService(SimpleMailMessage alertMailMessage) {
		RecoveryLogService.alertMailMessage = alertMailMessage;
	}
	
	public static void log(int level, String msg, final String error) {
		log.info(msg);
		if (CodeLookupUtils.serverConfig != null) {
			try {
				boolean isSend = false;
				switch (level) {
				case 1:
					if (missSent == null) {
						// sent immediately
						isSend = true;
						missSent = Calendar.getInstance().getTime();
					} else {
						// check for last time
						Date curr = Calendar.getInstance().getTime();
						Calendar calendar = Calendar.getInstance();
						calendar.setTime(missSent);
						calendar.add(Calendar.MINUTE, CodeLookupUtils.serverConfig.getInterval());
						Date temp = calendar.getTime();
						if (temp.compareTo(curr) <= 0) {
							isSend = true;
							missSent = curr;
						}
					}
					break;
				case 2:
					if (insertSent == null) {
						// sent immediately
						isSend = true;
						insertSent = Calendar.getInstance().getTime();
					} else {
						// check for last time
						Date curr = Calendar.getInstance().getTime();
						Calendar calendar = Calendar.getInstance();
						calendar.setTime(insertSent);
						calendar.add(Calendar.MINUTE, CodeLookupUtils.serverConfig.getInterval());
						Date temp = calendar.getTime();
						if (temp.compareTo(curr) <= 0) {
							isSend = true;
							insertSent = curr;
						}
					}
					break;
				case 3:
					if (calculateSent == null) {
						// sent immediately
						isSend = true;
						calculateSent = Calendar.getInstance().getTime();
					} else {
						// check for last time
						Date curr = Calendar.getInstance().getTime();
						Calendar calendar = Calendar.getInstance();
						calendar.setTime(calculateSent);
						calendar.add(Calendar.MINUTE, CodeLookupUtils.serverConfig.getInterval());
						Date temp = calendar.getTime();
						if (temp.compareTo(curr) <= 0) {
							isSend = true;
							calculateSent = curr;
						}
					}
					break;
				}

				if (isSend == true) {
					
					SimpleMailMessage mailMessage = new SimpleMailMessage(alertMailMessage);
					mailMessage.setSubject("[CDR SYSTEM ERROR]: system has error, please check");
					mailMessage.setText(error);
					mailMessage.setTo(CodeLookupUtils.serverConfig.getEmail());
					mailSender.send(mailMessage);
					
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}


}
